/** 摇一摇 主JS  */

CURRENT_TYPE_ID = null;
AUDIO_FLAG = true;

SHAKE_R = null;

$(function(){
	SHAKE_R = $('#r').val();
	//switchBottomMenu('menu_shake');
	var u = navigator.userAgent;
	if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
		var dom ="<audio id='msgAudio' src='/file/audio/newMsg.ogg' preload='auto'></audio>";
		$("#voice").append(dom);
	} else if (u.indexOf('iPhone') > -1 || u.indexOf("Safari") > -1) {//苹果手机
		var dom ="<audio id='msgAudio' src='/file/audio/newMsg.mp3' preload='auto'></audio>";
		$("#voice").append(dom);
	} /*else if (u.indexOf('Windows Phone') > -1) {//winphone手机
		alert("winphone手机");
	}*/
	init();
	loadGoodsAfterShake();
});


function init(){
	switchBottomMenu('menu_shake');
	var btn = $('.tit_525').width() - $(".tit_525 .label_box").width();
	var btnl = btn / 2;
	$(".tit_525 .label_box").css("margin-left", btnl + "px");
	if(localStorage){
		var shakeAudioFlag = localStorage.getItem("shakeAudioFlag");
		var $audioDom = $("#audio_btn");
		if(shakeAudioFlag == 2){
			AUDIO_FLAG = true;
			$audioDom.attr('role','1').text(' 关闭声音 ');
		}else{
			AUDIO_FLAG = false;
			$audioDom.attr('role','2').text(' 打开声音 ');
		}
	}
}

function toggleAudio(){
	var $audioDom = $("#audio_btn");
	var audio = $audioDom.attr('role');
	if(audio == 1){
		$audioDom.attr('role','2').text(' 打开声音 ');
		if(localStorage)localStorage.setItem("shakeAudioFlag",'1');
		AUDIO_FLAG = false;
	}else{
		$audioDom.attr('role','1').text(' 关闭声音 ');
		if(localStorage)localStorage.setItem("shakeAudioFlag",'2');
		AUDIO_FLAG = true;
	}
}

window.onload = function() {
    var myShakeEvent = new Shake({
        threshold: 15
    });
    myShakeEvent.start();
    window.addEventListener('shake', shakeEventDidOccur, false);
    function shakeEventDidOccur() {
    	if(AUDIO_FLAG)document.getElementById("shaking").play();
    	loadGoodsAfterShake();
    }
};

function switchGoodsType(typeId){
	var $item = $('#item_'+typeId);
	var $li = $('#ul_dom li');
	$li.removeClass('yao_selected');
	$('#ul_dom a').css('color','#ffffff');
	$('#ul_dom li').css('border','2px solid #920808');
	if(CURRENT_TYPE_ID != typeId){//未选中：执行选中操作
		$item.addClass('yao_selected');
		$('#item_'+typeId+' a').css('color','#1BF702');
		$('#item_'+typeId).css('border','2px solid #BF2E2E');
		CURRENT_TYPE_ID = typeId;
	}else{
		CURRENT_TYPE_ID = null;
	}
}


function loadGoodsAfterShake(){
	frontBaseLoadingOpen('正在加载藏品');//加载遮罩
	$("#goods_main").animate({top:"-1200px"});
	var param = {};
	if(CURRENT_TYPE_ID)param.typeId = CURRENT_TYPE_ID;
	$.post('/getGoodsForShake', param, function(rsp){
		frontBaseLoadingClose();//解除遮罩
		if(rsp){
			if(rsp.success){
				var data = rsp.goods;
				showGoods(data);
			}else{
				lh.alert(rsp.msg);
			}
		}
	},'json');
	
}

function showGoods(data){
	if(AUDIO_FLAG)document.getElementById("shaked").play();
	//$("#goodsShop").attr('href','/shop/'+data.shopId);
	$("#goodsPic").attr('src', data.picPath);
	var r = '';
	if(SHAKE_R){
		$("#a_goodsPic").attr('href', '/goods/'+data.id+'?shopId='+data.shopId+'&r='+SHAKE_R);
		$("#goodsShop").attr('href', '/sale/'+data.shopId+'?r='+SHAKE_R);
	}else{
		$("#a_goodsPic").attr('href', '/goods/'+data.id+'?shopId='+data.shopId);
		$("#goodsShop").attr('href', '/sale/'+data.shopId);
	}
	
	$("#shopName").text(data.shopName || '');
	$("#goodsName").text('藏品名称：'+(data.goodsName || ''));
	$("#goodsDesc").text(data.goodsDescription || '');
	var price = data.shopPrice;
	if(!price)price = '议价';
	$("#goodsPrice").text(price);
	$("#goods_main").animate({top:"0px"});
}







